/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.project.entity.ServiceConfigEntity;
import io.renren.modules.project.service.ServiceConfigService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.systemconfig.entity.ProjectBaseEntity;
import io.renren.modules.systemconfig.service.ProjectBaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/serviceconfig")
public class ServiceConfigController extends AbstractController {
	@Autowired
	private ServiceConfigService serviceconfigService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("serviceconfig:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = serviceconfigService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		return R.ok().put("server", serviceconfigService.getById(id));
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/save")
	@RequiresPermissions("serviceconfig:save")
	public R save(@RequestBody ServiceConfigEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);
		entity.setCreateTime(DateUtils.formatDefault(new Date()));
		serviceconfigService.saveEntity(entity);
		return R.ok();
	}
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/updateNewVersion")
	@RequiresPermissions("serviceconfig:save")
	public R updateNewVersion(@RequestBody ServiceConfigEntity entity){
		try{
			ValidatorUtils.validateEntity(entity, AddGroup.class);
			entity.setCreateTime(DateUtils.formatDefault(new Date()));
			serviceconfigService.updateNewVersion(entity);
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			return R.error("添加失败，请联系管理员！");
		}

	}


	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@PostMapping("/update")
	@RequiresPermissions("serviceconfig:update")
	public R update(@RequestBody ServiceConfigEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		serviceconfigService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@PostMapping("/delete")
	@RequiresPermissions("serviceconfig:delete")
	public R delete(@RequestBody Long[] delIds){
		serviceconfigService.deleteBatch(delIds);
		return R.ok();
	}
	@SysLog("删除用户")
	@PostMapping("/deleteVersion")
	@RequiresPermissions("serviceconfig:delete")
	public R delete(@RequestParam("id") Long deleteId){
		ServiceConfigEntity del = serviceconfigService.getById(deleteId);
		if(del==null){
			return R.error("配置信息不存在！");
		}
		if(del.getStatus()>0){
			return R.error("无法删除正在使用的配置信息");
		}
		serviceconfigService.removeById(deleteId);
		return R.ok();
	}

}
